package com.caigou.modules.forum.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.forum.bean.PacketDetailBean
import com.caigou.modules.forum.bean.PacketRecordBean
import com.caigou.modules.forum.repository.ForumRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2022/02/22
 */
@HiltViewModel
class PacketViewModel @Inject constructor(private val repository: ForumRepository) : ViewModel() {

    val packetRecordListLiveData = MutableLiveData<Resource<PacketRecordBean>>()
    val packetDetailLiveData = MutableLiveData<Resource<PacketDetailBean>>()

    fun getPacketRecordList(page: Int, limit: Int, redPacketId: Int) {
        packetRecordListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPacketRecordList(page, limit, redPacketId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PacketRecordBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    packetRecordListLiveData.value = it
                }
        }
    }

    fun getPacketRecordList2(redPacketId: Int) {
        packetRecordListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPacketRecordList2(redPacketId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PacketRecordBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    packetRecordListLiveData.value = it
                }
        }
    }


    fun getPacketDetail(redPacketId: Int, userId: String) {
        packetDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPacketDetail(redPacketId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PacketDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    packetDetailLiveData.value = it
                }
        }
    }

}